#include "stdio.h"
#include "SDL/SDL.h"
#include "stdlib.h"
int main(int argc, char *argv[]) {
	SDL_Surface *screen;
	SDL_Surface *acceille, *help, *rule;//This pointer will reference the backbuffer
	SDL_Surface *image,*cercle1,*cercle2,*cercle3,*cercle4,*cercle5,*cercle6,*cercle7,*cercle8,*cercle9;
	SDL_Surface *croix1,*croix2,*croix3,*croix4,*croix5,*croix6,*croix7,*croix8,*croix9;
	SDL_Surface *temp,*acc,*cercle,*croix, *win, *helps, *rules,*wins;//This pointer will temporarily reference our bitmap 	sprite
	SDL_Rect src,posacc,dest,dest1,dest2,dest3,dest4,dest5,dest6,dest7,dest8,dest9,pos1,destt;
	int numfenetre;
	int x,y,n=0;
	int A=0,B=0,C=0,D=0,E=0,F=0,G=0,H=0,I=0;
	int J=0,K=0,L=0,M=0,N=0,O=0,P=0,Q=0,R=0;
	int i=0;
	//We must first initialize the SDL video component, and check for success
	SDL_Event event;
    SDL_Init(SDL_INIT_VIDEO);
    screen= SDL_SetVideoMode(1152, 776, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
	SDL_WM_SetIcon(SDL_LoadBMP("img/icone.bmp"),NULL);
	SDL_WM_SetCaption("TIC-TAC-TOE !",NULL);
	SDL_SRCCOLORKEY;
     if (screen == NULL) {
		printf("Unable to set video mode: %s\n", SDL_GetError());
		return 1;}
	temp = SDL_LoadBMP("img/imagee.bmp");
	if (temp == NULL) {
		printf("Unable to load bitmap: %s\n", SDL_GetError());
		return 1;
	}
	//Load the bitmap into a temporary surface, and check for success
	cercle = SDL_LoadBMP("img/cercle.bmp");
	if (cercle == NULL) {
		printf("Unable to load bitmap: %s\n", SDL_GetError());
		return 1;
	}
		croix = SDL_LoadBMP("img/croixx.bmp");
	if (croix == NULL) {
		printf("Unable to load bitmap: %s\n", SDL_GetError());
		return 1;
	}
	SDL_SetColorKey(croix, SDL_SRCCOLORKEY, SDL_MapRGB(croix->format,0,0, 255));

			acc = SDL_LoadBMP("img/mi.bmp");
	if (acc == NULL) {
		printf("Unable to load bitmap: %s\n", SDL_GetError());
		return 1;
	}
	win = SDL_LoadBMP("img/win.bmp");
	if (win == NULL) {
		printf("Unable to load bitmap: %s\n", SDL_GetError());
		return 1;
	}
	help = SDL_LoadBMP("img/help.bmp");
	if (help == NULL) {
		printf("Unable to load bitmap: %s\n", SDL_GetError());
		return 1;
	}
	rule = SDL_LoadBMP("img/rules.bmp");
	if (rule == NULL) {
		printf("Unable to load bitmap: %s\n", SDL_GetError());
		return 1;
	}
//Load the bitmap into a temporary surface, and check for success
//Convert the surface to the appropriate display format
image = SDL_DisplayFormat(temp);
acceille = SDL_DisplayFormat(acc);
cercle1 = SDL_DisplayFormat(cercle);
cercle2 = SDL_DisplayFormat(cercle);
cercle3 = SDL_DisplayFormat(cercle);
cercle4 = SDL_DisplayFormat(cercle);
cercle5  = SDL_DisplayFormat(cercle);
cercle6  = SDL_DisplayFormat(cercle);
cercle7 = SDL_DisplayFormat(cercle);
cercle8 = SDL_DisplayFormat(cercle);
cercle9 = SDL_DisplayFormat(cercle);
croix1 =SDL_DisplayFormat(croix);
croix2 =SDL_DisplayFormat(croix);
croix3 =SDL_DisplayFormat(croix);
croix4 =SDL_DisplayFormat(croix);
croix5 =SDL_DisplayFormat(croix);
croix6 =SDL_DisplayFormat(croix);
croix7 =SDL_DisplayFormat(croix);
croix8 =SDL_DisplayFormat(croix);
croix9 =SDL_DisplayFormat(croix);
helps =SDL_DisplayFormat(help);
wins =SDL_DisplayFormat(win);
rules =SDL_DisplayFormat(rule);
//Release the temporary surface

SDL_FreeSurface(temp);
 SDL_FreeSurface(croix);
 SDL_FreeSurface(cercle);
 SDL_FreeSurface(acc);
 SDL_FreeSurface(helps);
 SDL_FreeSurface(wins);
 SDL_FreeSurface(rules);

//Construct the source rectangle for our blit
	src.x = 0;
	src.y = 0;
	src.w = image->w;	//Use image->w to display the entire width of the image
	src.h = image->h;	//Use image->h to display the entire height of the image
//Construct the destination rectangle for our blit
	dest.x = 0;//Display the image at the (X,Y) coordinates (100,100)
	dest.y = 0;
    posacc.x = 0;//Display the image at the (X,Y) coordinates (100,100)
	posacc.y = 0;
	src.w = acceille->w;	//Use image->w to display the entire width of the image
	src.h = acceille->h;
	destt.x = 20;//Display the image at the (X,Y) coordinates (100,100)
	destt.y = 0;
    posacc.x = 0;//Display the image at the (X,Y) coordinates (100,100)
	posacc.y = 0;
	src.w = acceille->w;	//Use image->w to display the entire width of the image
	src.h = acceille->h;
int done = 1;
numfenetre=1;
while(done)
{if (numfenetre==1)
{SDL_BlitSurface(acceille,&src,screen,&dest);
SDL_Flip(acceille);}
if(numfenetre==2)
{SDL_BlitSurface(image,&src,screen,&dest);
SDL_Flip(image);}
if (numfenetre==3)
{SDL_BlitSurface(help,&src,screen,&dest);
SDL_Flip(helps);}
if (numfenetre==4)
{SDL_BlitSurface(win,&src,screen,&dest);
SDL_Flip(win);}
if (numfenetre==5)
{SDL_BlitSurface(rule,&src,screen,&dest);
SDL_Flip(rule);}
while (SDL_PollEvent(&event)){
switch (event.type)
{case SDL_QUIT:
done=0;
case SDL_KEYDOWN:
if(event.key.keysym.sym==SDLK_ESCAPE)
{done=0;}
   case SDL_MOUSEBUTTONDOWN:
x=event.motion.x;
y=event.motion.y;
if(x<885 && x>421 && y>455 && y<571) {
SDL_Flip(screen);
numfenetre=2;
i=1;}
 if (x<861 && x>587 && y>271 && y<330) {

numfenetre=3;
SDL_Flip(screen);
i=2;
}


if (x<563 && x>279 && y>346 && y<407) {

numfenetre=5;
SDL_Flip(screen);
i=3;
}
if (x>578 && x<863 && y>346 && y<407) {

done=0;
}
if((i==2) ||(i==3)) {

       if (event.key.keysym.sym == SDLK_SPACE)
{
numfenetre=1;
SDL_Flip(screen);
i=1;}
}
 if(i==1) {
     switch (event.type)
   {

 case SDL_MOUSEBUTTONDOWN:
x=event.motion.x;
y=event.motion.y;
if(x<492 && x>335 && y>708 && y<772) {
SDL_Flip(screen);
numfenetre=3;
i=4;}

   }


if ((event.key.keysym.sym == SDLK_KP1)&&(J==0)&&(n%2==0))
{dest1.x = 273;
dest1.y = 204;

SDL_BlitSurface(cercle1, &src,image, &dest1);
A=1;
n++;}
if ((event.key.keysym.sym == SDLK_KP2)&&(K==0)&&(n%2==0))
{dest2.x = 512;
dest2.y = 204;
SDL_BlitSurface(cercle2, &src,image, &dest2);
B=1;
n++;}
if ((event.key.keysym.sym == SDLK_KP3)&&(L==0)&&(n%2==0))
{dest3.x = 783;
dest3.y = 204;
SDL_BlitSurface(cercle3, &src, image, &dest3);
C=1;
n++;}
if ((event.key.keysym.sym == SDLK_KP4)&&(M==0)&&(n%2==0))
{dest4.x = 273;
dest4.y = 360 ;
SDL_BlitSurface(cercle4, &src,image, &dest4);
D=1;
n++;}
if ((event.key.keysym.sym == SDLK_KP5)&&(N==0)&&(n%2==0))
{dest5.x = 512;
dest5.y = 360;
SDL_BlitSurface(cercle5, &src,image, &dest5);
E=1;
n++;}
if ((event.key.keysym.sym == SDLK_KP6)&&(O==0)&&(n%2==0))
{dest6.x = 783;
dest6.y = 360;
SDL_BlitSurface(cercle6, &src,image, &dest6);
F=1;
n++;}
if ((event.key.keysym.sym == SDLK_KP7)&&(P==0)&&(n%2==0))
{dest7.x = 273;
dest7.y = 514;
SDL_BlitSurface(cercle7, &src,image, &dest7);
G=1;
n++;}
if ((event.key.keysym.sym == SDLK_KP8)&&(Q==0)&&(n%2==0))
{dest8.x = 512;
dest8.y = 514;
SDL_BlitSurface(cercle8, &src,image, &dest8);
H=1;
n++;}
if ((event.key.keysym.sym == SDLK_KP9)&&(R==0)&&(n%2==0))
{dest9.x = 783;
dest9.y = 514;
SDL_BlitSurface(cercle9, &src,image, &dest9);
I=1;
n++;}
if ((event.key.keysym.sym == SDLK_1)&&(A==0)&&(n%2==1))
 {dest1.x = 273;
dest1.y = 204;

SDL_BlitSurface(croix1, &src, image, &dest1);
J=1;
n++;}
if((event.key.keysym.sym == SDLK_2)&&(B==0)&&(n%2==1))
{dest2.x = 512;
dest2.y = 204;
SDL_SetColorKey(croix2, SDL_SRCCOLORKEY, SDL_MapRGB(croix2->format,0,0, 204));
SDL_BlitSurface(croix2, &src, image, &dest2);
K=1;
n++;}
if ((event.key.keysym.sym == SDLK_3)&&(C==0)&&(n%2==1))
{dest3.x = 783;
dest3.y = 204;
SDL_BlitSurface(croix3, &src, image, &dest3);
L=1;
n++;}
if ((event.key.keysym.sym == SDLK_4)&&(D==0)&&(n%2==1))
{dest4.x = 273;
dest4.y = 360;
SDL_BlitSurface(croix4, &src, image, &dest4);
M=1;
n++;}
if ((event.key.keysym.sym == SDLK_5)&&(E==0)&&(n%2==1))
{dest5.x = 512;
dest5.y = 360;
SDL_BlitSurface(croix5, &src, image, &dest5);
N=1;
n++;}
if ((event.key.keysym.sym == SDLK_6)&&(F==0)&&(n%2==1))
{dest6.x = 783;
dest6.y = 360;
SDL_BlitSurface(croix6, &src, image, &dest6);
O=1;
n++;}
if ((event.key.keysym.sym == SDLK_7)&&(G==0)&&(n%2==1))
{dest7.x = 273;
dest7.y = 514;
SDL_BlitSurface(croix7, &src, image, &dest7);
P=1;
n++;}
if ((event.key.keysym.sym == SDLK_8)&&(H==0)&&(n%2==1))
{dest8.x = 512;
dest8.y = 514;
SDL_BlitSurface(croix8, &src, image, &dest8);
Q=1;
n++;}
if ((event.key.keysym.sym == SDLK_9)&&(I==0)&&(n%2==1))
{dest9.x = 783;
dest9.y = 514;
SDL_BlitSurface(croix9, &src, image, &dest9);
R=1;
n++;}
if(((A==1)&&(B==1)&&(C==1))
   ||((D==1)&&(E==1)&&(F==1))
   ||((G==1)&&(H==1)&&(I==1))
   ||((A==1)&&(D==1)&&(G==1))
   ||((B==1)&&(E==1)&&(H==1))
   ||((C==1)&&(F==1)&&(I==1))
   ||((A==1)&&(E==1)&&(I==1))
   ||((C==1)&&(E==1)&&(G==1)))
{SDL_BlitSurface(win,&src,image,&destt);
SDL_Flip(win);
i=5;}

}
if((i==4) &&(event.key.keysym.sym == SDLK_SPACE))
{
numfenetre=2;
SDL_Flip(screen);
i=1;}
 if(i==5) {
     switch (event.type)
   {

 case SDL_MOUSEBUTTONDOWN:
x=event.motion.x;
y=event.motion.y;
if(x<957 && x>640 && y>471 && y<584) {
SDL_Flip(screen);
numfenetre=1;
i=1;}
if(x<552 && x>370 && y>702 && y<773)
    done=0;
   }
break ;}
break;
}}
SDL_Flip(screen);
}
SDL_FreeSurface(screen);
return 0;}
